package com.cn.test;


import com.cn.config.JdbcConfig1;
import com.cn.config.SpringConfig;
import com.cn.pojo.Car;
import com.cn.pojo.JdbcDatasource;
import org.junit.Test;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;

import javax.sql.DataSource;

public class AnnotationTest3 {
    /**
     * 7.3 bean作用范围注解配置
     * @Scope("singleton")
     * @Scope("prototype")
     */
    @Test
    public void test1(){
        AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(SpringConfig.class);
        Car car1 = context.getBean("car1", Car.class);
        Car car2 = context.getBean("car1", Car.class);
        System.out.println("car1:"+car1);
        System.out.println("car2:"+car2);
    }

    /**
     * 7.4 bean生命周期注解配置
     */
    @Test
    public void test2(){
        AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(SpringConfig.class);
        Car car1 = context.getBean("car1", Car.class);
        context.registerShutdownHook();
    }

    /**
     * 7.5 @Value注入简单类型
     */
    @Test
    public void test3(){
        AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(SpringConfig.class);
        Car car1 = context.getBean("car1", Car.class);
        System.out.println(car1.getCarBrand());
    }

    /**
     * 7.6 @Value("${}")注入配置文件
     */
    @Test
    public void test4(){
        AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(SpringConfig.class);
        JdbcDatasource jdbcDatasource = context.getBean("jdbcDatasource", JdbcDatasource.class);
        System.out.println(jdbcDatasource.getUsername());
        System.out.println(jdbcDatasource.getPassword());
    }

    /**
     * 7.7 注解开发管理第三方Bean
     * 1. 单独定义配置类，使用一个方法返回需要注入的Bean对象，在配置类中使用@Import(JdbcConfig.class)注入，或者使用@ComponentScan({"com.itheima.config","com.itheima.service","com.itheima.dao"})，只要com.itheima.config包扫到了就行，三个包可以合并写成com.itheima
     */
    @Test
    public void test5(){
        AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(SpringConfig.class);
        DataSource dataSource = context.getBean("dataSource", DataSource.class);
    }

    /**
     * 7.8 注解开发管理第三方Bean,使用@Value("${xx}")注入配置文件中的设置
     */
    @Test
    public void test6(){
        AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(JdbcConfig1.class);
        DataSource dataSource = context.getBean("dataSource", DataSource.class);
    }

    /**
     * 7.9 注解开发管理第三方Bean,引用类型依赖注入
     * 1. 引用类型注入只需要为bean定义方法设置形参即可，容器会根据类型自动装配对象
     * 2. Spring会自动从IOC容器中找到BookDao对象赋值给参数bookDao变量，如果没有就会报错。
     */
    @Test
    public void test7(){
        AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(SpringConfig.class);
        DataSource dataSource = context.getBean("dataSource", DataSource.class);
    }





}
